package com.manage.controller;

import com.manage.pojo.Shoes;
import com.manage.pojo.operation.ShoesList;
import com.manage.service.ShoesService;
import com.manage.utils.FBJSONResult;
import com.manage.utils.PagedResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@Api(value = "有关鞋商品的操作", tags = {"有关鞋商品的操作"})
public class ShoesController {

    @Autowired
    private ShoesService shoesService;

    @ApiOperation(value = "鞋商品添加", notes = "鞋商品添加接口")
    @PostMapping("/shoes/shoesRegist")
    public FBJSONResult bookRegist(@RequestBody Shoes shoes) throws Exception {
        shoes.setUserId(shoes.getUserId());
        shoes.setShoesType(shoes.getShoesType());
        shoes.setShoesName(shoes.getShoesName());
        shoes.setShoesImage(shoes.getShoesImage());
        shoes.setShoesNumber(shoes.getShoesNumber());
        shoes.setShoesPrice(shoes.getShoesPrice());
        shoes.setShoesMonthNumber(shoes.getShoesMonthNumber());
        shoes.setShoesDiscsss(shoes.getShoesDiscsss());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:dd");
        shoes.setCreateTime(sdf.format(new Date()));
        shoesService.saveShoes(shoes);
        return FBJSONResult.ok(shoes);
    }

    @ApiOperation(value = "鞋商品删除", notes = "鞋商品信息删除接口")
    @ApiImplicitParam(name = "shoesId", value = "鞋id", required = true, dataType = "String", paramType = "query")
    @PostMapping("/shoes/shoesDelete")
    public FBJSONResult shoesDelete(String shoesId) throws Exception {
        if(StringUtils.isBlank(shoesId)) {
            return FBJSONResult.errorMsg("鞋id不能为空");
        }
        //判断鞋id是否存在
        boolean shoesIdIsExit = shoesService.queryShoesIsExit(shoesId);
        if(shoesIdIsExit) {
            shoesService.shoesDelete(shoesId);
        }
        return FBJSONResult.ok("删除成功");
    }

    @ApiOperation(value = "鞋更新", notes = "鞋更新接口")
    @PostMapping("/shoes/shoesUpdate")
    public FBJSONResult shoesUpdate(@RequestBody Shoes shoes) throws Exception {

        //判断用户ID是否存在
        boolean shoesIdIsExit = shoesService.queryShoesIsExit(shoes.getShoesId());
        if(shoesIdIsExit) {
            Shoes shoesExample = new Shoes();
            shoesExample.setShoesId(shoes.getShoesId());
            shoesExample.setUserId(shoes.getUserId());
            shoesExample.setShoesType(shoes.getShoesType());
            shoesExample.setShoesName(shoes.getShoesName());
            shoesExample.setShoesImage(shoes.getShoesImage());
            shoesExample.setShoesNumber(shoes.getShoesNumber());
            shoesExample.setShoesPrice(shoes.getShoesPrice());
            shoesExample.setShoesMonthNumber(shoes.getShoesMonthNumber());
            shoesExample.setShoesDiscsss(shoes.getShoesDiscsss());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:dd");
            shoesExample.setCreateTime(sdf.format(new Date()));
            shoesService.updateShoesInfo(shoesExample);
            return FBJSONResult.ok(shoesExample);
        } else {
            return FBJSONResult.errorMsg("更新失败");
        }
    }

    @ApiOperation(value = "鞋商品信息查询(根据鞋id)", notes = "鞋商品信息查询(根据鞋id)接口")
    @ApiImplicitParam(name = "shoesId", value = "鞋id", required = true, dataType = "String", paramType = "query")
    @PostMapping("/shoes/queryShoesInfoByShoesId")
    public FBJSONResult queryShoesInfoByShoesId(String shoesId) throws Exception {

        if(StringUtils.isBlank(shoesId)) {
            return FBJSONResult.errorMsg("鞋id不能为空");
        }
        ShoesList shoesResult = shoesService.queryShoesByShoesId(shoesId);
        return FBJSONResult.ok(shoesResult);
    }

    @ApiOperation(value = "鞋商品列表查询(根据鞋类别)", notes = "鞋商品列表查询(根据鞋类别)接口")
    @ApiImplicitParam(name = "shoesType", value = "鞋类别", required = true, dataType = "String", paramType = "query")
    @PostMapping("/shoes/queryShoesInfoListByShoesType")
    public FBJSONResult queryShoesInfoListByShoesType(Integer page, String shoesType) throws Exception {
        if(StringUtils.isBlank(shoesType)) {
            return FBJSONResult.errorMsg("鞋类别不能为空");
        }
        if(page == null) {
            page = 1;
        }
        PagedResult result = shoesService.queryShoesByShoesType(page, 10, shoesType);
        return FBJSONResult.ok(result);
    }

    @ApiOperation(value = "鞋商品列表查询(根据用户id)", notes = "鞋商品列表查询(根据用户id)接口")
    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String", paramType = "query")
    @PostMapping("/shoes/queryShoesInfoListByUserId")
    public FBJSONResult queryShoesInfoListByUserId(Integer page, String userId) throws Exception {
        if(StringUtils.isBlank(userId)) {
            return FBJSONResult.errorMsg("用户id不能为空");
        }
        if(page == null) {
            page = 1;
        }
        PagedResult result = shoesService.queryShoesByUserId(page, 10, userId);
        return FBJSONResult.ok(result);
    }

    @ApiOperation(value = "鞋商品列表全部查询", notes = "鞋商品列表全部查询接口")
    @PostMapping("/shoes/queryShoesInfoListAll")
    public FBJSONResult queryShoesInfoListAll(Integer page) throws Exception {
        if(page == null) {
            page = 1;
        }
        PagedResult result = shoesService.queryAllShoes(page, 10);
        return FBJSONResult.ok(result);
    }
}
